package com.gljx.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gljx.util.CommonExport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gljx.util.GetFid;
import com.gljx.util.GetFnumber;
import com.gljx.util.TimeFormat;
import com.gljx.web.Entity.Department;
import com.gljx.web.Entity.SystemParameter;
import com.gljx.web.Entity.User;
import com.gljx.web.service.CustomLogService;
import com.gljx.web.service.DepartmentService;
import com.gljx.web.service.SystemParameterService;

@Controller
@RequestMapping("/")
public class SysetmParamenterController {

	//自动注入
	@Autowired
	private SystemParameterService systemParameterService;
	
	//自动注入
	@Autowired
	private CustomLogService customLogService;
	
	//自动注入
	@Autowired
	private DepartmentService departmentService;
	
	/**
	 * 根据公司id查询
	 * @param request
	 * @return
	 */
	@RequestMapping("/querySysetmParamenter/look.action")
    public String querySysetmParamenter(HttpServletRequest request, String flag) {
        //获取操作人信息
	    User user = (User)request.getSession().getAttribute("user");
	    //查询数据
	    List<SystemParameter> systemParameters = systemParameterService.querySysParameterByFcontrolunitId(user.getFcompanyid());
	    //把数据传到页面
	    request.setAttribute("systemParameters", systemParameters);
        request.setAttribute("flag", flag);
        return "/backHome/systemParameter/systemParameterList";
	}
	
	/**
	 * 根据名称查询
	 * @param request
	 * @param fnameL2 名称
	 * @return
	 */
	@RequestMapping("/querySysetmParamenterByFnameL2")
	public String querySysetmParamenterByFnameL2(HttpServletRequest request,String fnameL2){
		//获取操作人信息
	    User user = (User)request.getSession().getAttribute("user");
		if("".equals(fnameL2)){
            querySysetmParamenter(request, null);//未空就调用查询的方法
        }else{
            //去除空格
            fnameL2 = fnameL2.trim();
            //查询数据
			List<SystemParameter> systemParameters = systemParameterService.querySysetmParamenterByFnameL2(user.getFcompanyid(),fnameL2);
			//把数据传到页面
			request.setAttribute("systemParameters", systemParameters);
			request.setAttribute("fnameL2", fnameL2);
		}
		return "/backHome/systemParameter/systemParameterList";
	}
	
	/**
	 * 跳转到添加页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/insertSysetmParamenter")
	public String insertSysetmParamenter(HttpServletRequest request,String type){
		//获取操作人信息
	    User user = (User)request.getSession().getAttribute("user");
	    //获取组织
	    Department department = departmentService.queryAdminByFid(user.getFcompanyid());
	    //获取上一个编码
        /*String fnumber = customLogService.selectFunmber("ct_ds_systemparameter",user.getFcompanyid());*/
	    //获取新的编码
        String number = GetFnumber.fnumbers("XTCSSZ");
	    //把数据传到页面
	    request.setAttribute("fnumber", number);
	    request.setAttribute("department", department);
	    request.setAttribute("type", type);
		return "/backHome/systemParameter/editSystemParameter";
	}
	
	/**
	 * 添加，修改系统参数
	 * @param request
	 * @param systemParameter 属性自动封装
	 * @return
	 */
	@RequestMapping("/editSysetmParamenter")
	public String editSysetmParamenter(HttpServletRequest request,SystemParameter systemParameter){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//控制单元id
		systemParameter.setFcontrolunitid(user.getFcompanyid());
		//公司id
		systemParameter.setCfficompanyid(user.getFcompanyid());
		//获取当前时间
		String time = TimeFormat.timeFormat(new Date());
		if("".equals(systemParameter.getFid()) || systemParameter.getFid() == null){
			//利用UUID生成fid
			systemParameter.setFid(GetFid.UUIDIntoFid());
			//创建人id
			systemParameter.setFcreatorid(user.getFid());
			//创建时间
			systemParameter.setFcreatetime(time);
			//添加
			systemParameterService.addSystemParameter(systemParameter);
			//添加日志
			customLogService.addLog("系统参数", time+"对系统参数设置进行添加操作", null, user.getFid());
			//重定向到添加页面
			return "redirect:/DrivingSys/insertSysetmParamenter?type=3";
		}else{
			//修改人id
			systemParameter.setFlastupdateuserid(user.getFid());
			//修改时间
			systemParameter.setFlastupdatetime(time);
			//修改
			systemParameterService.mofidySystemParameter(systemParameter);
			//添加日志
			customLogService.addLog("系统参数", time+"对系统参数设置进行修改操作", null, user.getFid());
			//调用查询的方法
            return commonController.skipUrl("/querySysetmParamenter/look.action");
        }
		
	}
	
	/**
	 * 根据fid查询
	 * @param request
	 * @param fid 要查询的fid
	 * @return
	 */
	@RequestMapping("/selectSystemParameterByFid")
	public String selectSystemParameterByFid(HttpServletRequest request,String fid){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//获取组织
	    Department department = departmentService.queryAdminByFid(user.getFcompanyid());
	    //查询数据
	    SystemParameter systemParameter = systemParameterService.quertSystemParameterByFid(fid);
	    //把数据传到页面
	    request.setAttribute("systemParameter", systemParameter);
	    request.setAttribute("department", department);
		return "/backHome/systemParameter/editSystemParameter";
	}
	
	/**
	 * 根据fid删除
	 * @param request
	 * @param deleteId 要删除的fid，可以多个
	 * @return
	 */
	@RequestMapping("/deleteSysetmParamenterByFid")
	public String deleteSysetmParamenterByFid(HttpServletRequest request,String deleteId[],String fid){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//删除
		if(fid == null || "".equals(fid)){
			for (int i = 0; i < deleteId.length; i++) {
				systemParameterService.deleteSystemParameter(deleteId[i]);
			}
		}else{
			systemParameterService.deleteSystemParameter(fid);
		}
		//添加日志
		customLogService.addLog("系统参数", TimeFormat.timeFormat(new Date())+"对系统参数设置进行删除操作", null, user.getFid());
		//调用查询的方法
        querySysetmParamenter(request, null);
        return "/backHome/systemParameter/systemParameterList";
	}
	
	/**
	 * 查询规费
	 * @param xzlx
	 * @return
	 */
	@RequestMapping("/selGuiFei")
	@ResponseBody
	public String selGuiFei(String xzlx,HttpServletRequest request){
		List<SystemParameter> systemParameter = new ArrayList<>();
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		String guiFei;
		String type = xzlx.substring(0, 1);
		if(type.equals("C")){
			systemParameter = systemParameterService.querySysetmParamenterByFnameL2(user.getFcompanyid(), "规费金额(小车)");
			if (!systemParameter.isEmpty()) {
                guiFei = systemParameter.get(0).getCfnumbers().toString();
				if(guiFei==null||guiFei.equals("")) {
					guiFei="0";
				} else {

				}
			}else {
				guiFei="0";
			}
			
		}else if(type.equals("B") || type.equals("A")){
			systemParameter = systemParameterService.querySysetmParamenterByFnameL2(user.getFcompanyid(), "规费金额(大车)");
			if (!systemParameter.isEmpty()) {
                guiFei = systemParameter.get(0).getCfnumbers().toString();
				if (guiFei == null || guiFei.equals("")) {
					guiFei = "0";
				} else {

				}
			}else {
				guiFei="0";
			}
		}else{
			systemParameter = systemParameterService.querySysetmParamenterByFnameL2(user.getFcompanyid(), "规费金额(小车)");
			if (!systemParameter.isEmpty()) {
                guiFei = systemParameter.get(0).getCfnumbers().toString();
				if(guiFei==null||guiFei.equals("")) {
					guiFei="0";
				}
			}else {
				guiFei="0";
			}
		}
		return  guiFei;
	}
	
	/**
	 * 查询规费
	 * @param xzlx
	 * @return
	 */
	@RequestMapping("/YhPrice")
	@ResponseBody
	public String selYhPrice(HttpServletRequest request){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		SystemParameter prices = systemParameterService.querySysetmParamenterByFnameL2(user.getFcompanyid(), "增加优惠金额").get(0);
		String price = prices.getCfnumbers().toString(); 
		return  price;
	}
	
	
	/**
	 * 查询补考收费费用
	 * @param state 学员状态
	 * @param request
	 * @return
	 */
	
	@RequestMapping("/selMakeupChatge")
	@ResponseBody
	public String selMakeupChatge(String state,HttpServletRequest request){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		String charge;
		if("3".equals(state) || "2".equals(state)){
			List<SystemParameter> systemParameters = systemParameterService.querySysetmParamenterByFnameL2(user.getFcompanyid(), "补考收费（科目一）");
			if (!systemParameters.isEmpty()) {
				charge = systemParameters.get(0).getCfnumbers().toString();
			} else {
				charge = "0";
			}

        } else if ("5".equals(state) || "6".equals(state) || "4".equals(state) || "17".equals(state) || "18".equals(state) || "22".equals(state)) {
            List<SystemParameter> systemParameters = systemParameterService.querySysetmParamenterByFnameL2(user.getFcompanyid(), "补考收费（科目二）");
			if (!systemParameters.isEmpty()) {
				charge = systemParameters.get(0).getCfnumbers().toString();
			} else {
				charge = "0";
			}
        } else if ("7".equals(state) || "8".equals(state) || "9".equals(state) || "15".equals(state) || "16".equals(state) || "20".equals(state) || "21".equals(state)) {
            List<SystemParameter> systemParameters = systemParameterService.querySysetmParamenterByFnameL2(user.getFcompanyid(), "补考收费（科目三）");
			if (!systemParameters.isEmpty()) {
				charge = systemParameters.get(0).getCfnumbers().toString();
			} else {
				charge = "0";
			}
		}else{
			charge = "0";
		}
		return charge;
	}

	@RequestMapping("/yzSystemParameter")
	@ResponseBody
	public Boolean yzSystemParameter(String name, HttpServletRequest request) {
		//获取操作人信息
		User user = (User) request.getSession().getAttribute("user");
		SystemParameter systemParameter = systemParameterService.querySysetmParamenterByName(user.getFcompanyid(), name);
		if (systemParameter == null) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 导出Execl
	 *
	 * @param response
	 * @param request
	 * @param fnameL2  名称
	 */
	@RequestMapping("/importSysParameter")
	public void importSysParameter(HttpServletResponse response, HttpServletRequest request, String fnameL2) {
		//获取操作人信息
		User user = (User) request.getSession().getAttribute("user");
		//定义接收类型
		List<Object> data = new ArrayList<>();
		//查询数据
        List<SystemParameter> systemParameters = systemParameterService.querySysetmParamenterByFnameL2(user.getFcompanyid(), fnameL2.trim());
        for (SystemParameter systemParameter : systemParameters) {
			data.add(systemParameter.getFnumber());
			data.add(systemParameter.getFnameL2());
			data.add(systemParameter.getFsimplename());
			data.add(systemParameter.getFdescriptionL2());
			data.add(systemParameter.getFcreatetime());
			data.add(systemParameter.getFlastupdatetime());
			data.add(systemParameter.getCreateName());
			data.add(systemParameter.getUpdateName());
			if (systemParameter.getCfnumbers() == null) {
				data.add(null);
			} else {
				data.add(systemParameter.getCfnumbers());
			}
			if (systemParameter.getCfenabled() == 1) {
				data.add("是");
			} else {
				data.add("否");
			}
		}
		//头部数据
		String head[] = {"编码", "名称", "简称", "描述", "创建时间", "最后修改时间", "创建者", "修改人", "数值", "启用"};
		//长度
		Integer length[] = {6500, 7000, 6000, 6000, 6000, 6000, 3000, 3000, 2000, 2000};
		try {
			//导出Excel
			CommonExport.importExcel(response, "系统参数设置" + TimeFormat.getNowTime2() + ".xls", "系统参数设置", data, head, length);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
